package com.mask.im.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mask.im.business.model.entity.ComplaintsCauseDO;
import com.mask.im.business.model.entity.Notify;
import com.mask.im.business.model.entity.UserComplaints;
import com.mask.im.business.model.entity.UserInfo;
import com.mask.im.business.mapper.UserComplaintsMapper;
import com.mask.im.business.model.dto.ComplaintsDTO;
import com.mask.im.business.model.vo.ConditionVO;
import com.mask.im.business.common.model.PageResult;
import com.mask.im.business.common.model.RestResponse;
import com.mask.im.business.service.ComplaintsCauseService;
import com.mask.im.business.service.NotifyService;
import com.mask.im.business.service.UserComplaintsService;
import com.mask.im.business.service.UserInfoService;
import com.mask.im.business.common.util.PageUtil;
import com.mask.im.business.common.util.MaskIMSecurityUtils;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import static com.mask.im.business.common.constant.CommonConstant.COMPLAINTS_TITLE;


@Service
public class UserComplaintsServiceImpl extends ServiceImpl<UserComplaintsMapper, UserComplaints> implements UserComplaintsService {

	@Autowired
	private UserInfoService userInfoService;

	@Autowired
	private ComplaintsCauseService complaintsCauseService;

	@Autowired
	private NotifyService notifyService;

	@Override
	public RestResponse<String> saveUserComplaints(UserComplaints userComplaints) {
		Integer userInfo = MaskIMSecurityUtils.getUserId();
		userComplaints.setComplainant(userInfo);
		userComplaints.setStatus(0);
		int insert = this.baseMapper.insert(userComplaints);
		if (insert > 0) {
			return RestResponse.ok("success");
		}
		return RestResponse.fail("error");
	}

	@SneakyThrows
	@Override
	public PageResult<ComplaintsDTO> complaintsList(ConditionVO conditionVO) {
		CompletableFuture<Integer> complaintsListCount = CompletableFuture.supplyAsync(() ->
				this.baseMapper.complaintsListCount(conditionVO));
		List<ComplaintsDTO> complaintsDTOS = this.baseMapper.complaintsList(conditionVO, PageUtil.getCurrentPage(), PageUtil.getSize());
		return PageResult.<ComplaintsDTO>builder()
				.data(complaintsDTOS).total(complaintsListCount.get()).build();
	}

	@Override
	@Transactional
	public RestResponse<String> changeStatus(ConditionVO conditionVO) {
		Integer adminId = MaskIMSecurityUtils.getUserId();
		UserComplaints complaints = UserComplaints.builder()
				.id(conditionVO.getComplaintsId())
				.status(conditionVO.getStatus())
				.result(conditionVO.getResult())
				.processors(adminId)
				.feedbackContent(conditionVO.getDesc())
				.build();
		boolean update = this.updateById(complaints);
		if (complaints.getStatus() == 2) {
			UserComplaints userComplaints = this.getById(conditionVO.getComplaintsId());
			ComplaintsCauseDO complaintsCause = complaintsCauseService.getById(userComplaints.getCauseId());
			UserInfo userInfo = userInfoService.getById(userComplaints.getRespondent());
			Notify notify = Notify.builder()
					.notifyTitle(COMPLAINTS_TITLE)
					.notifyContent(speechTemplate(userComplaints.getResult(), userInfo.getUsername(),
							userComplaints.getCreateTime(), complaintsCause.getName(), userComplaints.getFeedbackContent()))
					.receiver(userComplaints.getComplainant())
					.publisher(adminId)
					.sort(5)
					.build();
			notifyService.save(notify);
		}
		if (update) {
			return RestResponse.ok();
		}
		return RestResponse.fail();
	}

	private String speechTemplate(Integer result, String respondentName, LocalDateTime time, String cause, String feedbackContent) {
		String s;
		if (result.equals(0)) {
			s = "您在" + time.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日")) + "所投诉的用户 " + respondentName + " 因《" + cause +
					"》，经过我们的仔细的审核，得到的最终结果为《投诉不通过》，原因：" + feedbackContent + "。如需继续投诉，请提供更为详细的证据，如有结果我们将第一时间通知您。如有不便，希望理解。";

		} else if (result.equals(1)) {
			s = "您在" + time.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日")) + "所投诉的用户 " + respondentName +
					" 经过我们的仔细的审核，得到的最终结果为《投诉通过》，非常感谢您的投诉反馈，我们将对" + respondentName + "做出相应的处罚。希望您今后为我们提供更多的宝贵意见。";
		} else {
			s = "您在" + time.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日")) + "所投诉的用户 " + respondentName +
					" 经过我们的仔细的审核，得到的最终结果为《继续观察》，原因：" + feedbackContent + "。非常感谢您的投诉反馈，我们期望收到您更多的证据，希望理解。";
		}
		return s;
	}
}

